package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user/shoppingCart")
@RestController("adminShoppingCartController")
@Slf4j
@Api(tags = "用户端购物车模块")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 添加购物车
     * @return
     */
    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车:{}",shoppingCartDTO);
        Long id=BaseContext.getCurrentId();
        shoppingCartService.add(shoppingCartDTO);
        return Result.success();
    }


    /**
     * 查看购物车
     * @return
     */
    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>>list(){
        log.info("用户{}查看购物车",BaseContext.getCurrentId());
        List<ShoppingCart>shoppingCartList=shoppingCartService.list();
        return Result.success(shoppingCartList);
    }


    /**
     * 清空购物车
     */
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result clean(){
        log.info("用户{}清空了购物车",BaseContext.getCurrentId());
        shoppingCartService.clean();
        return Result.success();
    }


    /**
     * 删除购物车的一个商品
     */
    @ApiOperation("删除购物车的一个商品")
    @PostMapping("/sub")
    public Result deleteOne(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("用户{}删除了购物车的商品{}",BaseContext.getCurrentId(),shoppingCartDTO);
        shoppingCartService.deleteOne(shoppingCartDTO);
        return Result.success();
    }

}
